package com.lncg.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 数据源配置
 * 简化版本，只配置必要的连接池
 */
@Configuration
public class DataSourceConfig {
    
    @Autowired
    private AppConfig appConfig;
    
    /**
     * SQL Server数据源
     */
    @Bean(name = "sqlServerDataSource")
    public DataSource sqlServerDataSource() {
        try {
            HikariConfig config = new HikariConfig();
            config.setJdbcUrl(appConfig.getSqlServerUrl());
            config.setUsername(appConfig.getSqlServerUsername());
            config.setPassword(appConfig.getSqlServerPassword());
            config.setDriverClassName(appConfig.getSqlServerDriver());
            config.setMaximumPoolSize(5);
            config.setMinimumIdle(1);
            config.setConnectionTimeout(30000);
            config.setIdleTimeout(600000);
            config.setMaxLifetime(1800000);
            config.setLeakDetectionThreshold(60000);
            
            // JTDS驱动不支持isValid()方法，使用连接测试查询
            config.setConnectionTestQuery("SELECT 1");
            config.setValidationTimeout(5000);
            
            System.out.println("✅ SQL Server 2005数据源配置完成");
            return new HikariDataSource(config);
        } catch (Exception e) {
            System.err.println("❌ SQL Server数据源配置失败: " + e.getMessage());
            throw new RuntimeException("SQL Server数据源配置失败", e);
        }
    }
    
    /**
     * MySQL数据源
     */
    @Bean(name = "mysqlDataSource")
    public DataSource mysqlDataSource() {
        try {
            HikariConfig config = new HikariConfig();
            config.setJdbcUrl(appConfig.getMysqlUrl());
            config.setUsername(appConfig.getMysqlUsername());
            config.setPassword(appConfig.getMysqlPassword());
            config.setDriverClassName(appConfig.getMysqlDriver());
            config.setMaximumPoolSize(5);
            config.setMinimumIdle(1);
            config.setConnectionTimeout(30000);
            config.setIdleTimeout(600000);
            config.setMaxLifetime(1800000);
            config.setLeakDetectionThreshold(60000);
            
            System.out.println("✅ MySQL数据源配置完成");
            return new HikariDataSource(config);
        } catch (Exception e) {
            System.err.println("❌ MySQL数据源配置失败: " + e.getMessage());
            throw new RuntimeException("MySQL数据源配置失败", e);
        }
    }
    
    /**
     * SQL Server 2016数据源 (NBIotDB)
     */
    @Bean(name = "sqlServer2016DataSource")
    public DataSource sqlServer2016DataSource() {
        try {
            HikariConfig config = new HikariConfig();
            config.setJdbcUrl(appConfig.getSqlServer2016Url());
            config.setUsername(appConfig.getSqlServer2016Username());
            config.setPassword(appConfig.getSqlServer2016Password());
            config.setDriverClassName(appConfig.getSqlServer2016Driver());
            config.setMaximumPoolSize(5);
            config.setMinimumIdle(1);
            config.setConnectionTimeout(30000);
            config.setIdleTimeout(600000);
            config.setMaxLifetime(1800000);
            config.setLeakDetectionThreshold(60000);
            
            System.out.println("✅ SQL Server 2016数据源配置完成");
            return new HikariDataSource(config);
        } catch (Exception e) {
            System.err.println("❌ SQL Server 2016数据源配置失败: " + e.getMessage());
            throw new RuntimeException("SQL Server 2016数据源配置失败", e);
        }
    }
}